﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Silverlight.Library;

namespace FRL.DebtChased.AgentEditor
{

	/// <summary>
	/// Declaration of MainPage class
	/// </summary>
	public partial class MainPage : UserControl, IDebtChasedMainPlugInWindow
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		private static MainPage m_singleton = null;
		public static MainPage Singleton
		{
			get { return m_singleton; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public MainPage()
		{

			// Initialize member variables
			m_singleton = this;

			// Load xaml resources
			InitializeComponent();

			// Setup library session
			new LibrarySession(this);

			// Do the initial load of support data
			_initialDataLoad();

		}

		/////////////////////////////////////////////////////////////
		// Implementation of IDebtChasedMainPlugInWindow

		public void ShowHourglass(bool bTrue)
		{

			// Show/hide hourglass
			gridHourglass.IncShowCount(bTrue);

		}

		public void WebsiteMenuClicked(DebtChasedWebsiteMenuCtrl.eMenuItem menuItemId)
		{
            MessageBox.Show("Menu Click for " + menuItemId.ToString(), "Menu Click Handler", MessageBoxButton.OK);
		}

		public void ContactPointButtonClick(PointActiveButton button, int iContactId, Guid contactPointId)
		{
            MessageBox.Show("Contact Point Click" , "Contact Point Click Handler", MessageBoxButton.OK);
        }


		private void _initialDataLoad()
		{

			// If the supporting data is not loaded...
			if (CachedData.Singleton.CountryMap == null)
			{

				// Show hourglass
				gridHourglass.IncShowCount(true);

				// Load real or fake supporting data
#if USEFAKEDATA

				// Create event
				object[] results = new object[1];
				results[0] = CachedData.GetDummySupportingData();
				GetAgentEditorSupportDataCompletedEventArgs e = new GetAgentEditorSupportDataCompletedEventArgs(
					results,
					null,
					false,
					null
				);

				// Call completed event
				proxy_GetAgentEditorSupportDataCompleted(null, e);

#else

				// Start by downloading the supporting data
				RichContentServiceClient proxy = new RichContentServiceClient();
				proxy.GetAgentEditorSupportDataCompleted += new EventHandler<GetAgentEditorSupportDataCompletedEventArgs>(proxy_GetAgentEditorSupportDataCompleted);
				proxy.Endpoint.Address = ExtensionsForSilverlight.CreateDebtChasedEndpoint();
				proxy.GetAgentEditorSupportDataAsync();

#endif // #if USEFAKEDATA

			}
			else
			{
                AgentPain.PostLoad();


			}

		}

		void proxy_GetAgentEditorSupportDataCompleted(object sender, GetAgentEditorSupportDataCompletedEventArgs e)
		{

			// Hide hourglass
			gridHourglass.IncShowCount(false);

			// Get the result
			if (e.Error != null)
				MessageBoxes.PopupMessage(e.Error.Message);
			else if (e.Result == null)
				MessageBoxes.PopupMessage_InternalError("Failed to load supporting data from server.");
			else
			{

				// Update the cached data
				CachedData.Singleton.AddIncoming(e.Result);

                AgentPain.PostStartDataLoad();


				// Call loading function again
				_initialDataLoad();

			}

			// Setup controls
            AgentPain.SetupControls();

		}

	}

}
